home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 February: Tool Chest / Dev.CD Feb 95 / Dev.CD Feb 95.toast / Sample Code / Snippets / Toolbox / Password / Password.c next >
Encoding:
Text File  |  1992-07-15  |  7.3 KB  |  273 lines  |  [TEXT/MPS ]

  1. //
  2. //    Password
  3. //     Sample of three different ways to implement a password dialog
  4. //
  5. //    Tim Dierks, UK Mac DTS
  6. //    August, 1991
  7. //
  8.  
  9. //    This file contains all the interesting password stuff:
  10. //   Sample.c is just the application shell, derived from CSample
  11.  
  12. #include <Types.h>
  13. #include <Memory.h>
  14. #include <Resources.h>
  15. #include <OSUtils.h>
  16. #include <Quickdraw.h>
  17. #include <Fonts.h>
  18. #include <Events.h>
  19. #include <OSEvents.h>
  20. #include <Windows.h>
  21. #include <Menus.h>
  22. #include <Dialogs.h>
  23. #include <TextEdit.h>
  24.  
  25. #include "Sample.h"
  26. #include "Password.h"
  27.  
  28. void
  29. DisplayPassword(char *password)
  30. {    DialogPtr    dlog;
  31.     short        item;
  32.     
  33.     dlog = GetNewDialog(rDisplayPasswordDialog,0L,(WindowPtr) -1L);
  34.     
  35.     ParamText(password,0L,0L,0L);
  36.     
  37.     do
  38.     {    ModalDialog(0L,&item);
  39.     } while (item != 1);            // Until the OK button gets hit
  40.     
  41.     DisposDialog(dlog);
  42. }
  43.  
  44. char *
  45. TwoItemDialog()
  46. {    static char        password[256];
  47.     DialogPtr        dlog;
  48.     Handle            itemH;
  49.     ControlHandle    chkBox;
  50.     short            item,itemType,chkVal;
  51.     Rect            box;
  52.     Point            size;
  53.     
  54.     dlog = GetNewDialog(rTwoItemDialog,0L,(WindowPtr) -1L);
  55.     
  56.     do
  57.     {    ModalDialog(TwoItemFilter,&item);
  58.         if (item == 4)                                            // Hide/show checkbox
  59.         {    GetDItem(dlog,4,&itemType,(Handle*)&chkBox,&box);    // Get check value
  60.             chkVal = !GetCtlValue(chkBox);
  61.             SetCtlValue(chkBox,chkVal);                            // Invert it
  62.             size.v = dlog->portRect.bottom - dlog->portRect.top;
  63.             size.h = dlog->portRect.right - dlog->portRect.left;
  64.             if (chkVal)
  65.                 size.v += 35;
  66.             else
  67.                 size.v -= 35;
  68.             
  69.             SizeWindow(dlog,size.h,size.v,true);        // Resize window
  70.         }
  71.     } while (item != 1);            // Until the OK button is hit
  72.     
  73.     GetDItem(dlog,3,&itemType,&itemH,&box);        // Get text from hidden dialog item
  74.     GetIText(itemH,password);
  75.     
  76.     DisposDialog(dlog);
  77.     
  78.     return password;
  79. }
  80.  
  81. pascal Boolean
  82. TwoItemFilter(DialogPtr dlog,EventRecord *event,short *itemHit)
  83. {    DialogPtr    evtDlog;
  84.     short        selStart,selEnd;
  85.     
  86.     if (event->what == keyDown || event->what == autoKey)
  87.     {    switch (event->message & charCodeMask)
  88.         {    case '\n':            // Return  (hitting return or enter is the same as hitting the OK button)
  89.             case '\003':        // Enter
  90.                 *itemHit = 1;        // OK Button
  91.                 return true;        // We handled the event
  92.             case '\t':            // Tab
  93.                 event->what = nullEvent;    // Do nothing (don't let the user tab to the hidden field)
  94.                 return false;
  95.             case '\034':        // Left arrow  (Keys that just change the selection)
  96.             case '\035':        // Right arrow
  97.             case '\036':        // Up arrow
  98.             case '\037':        // Down arrow
  99.                 return false;            // Let ModalDialog handle them
  100.             default:
  101.                 selStart = (**((DialogPeek)dlog)->textH).selStart;        // Get the selection in the visible item
  102.                 selEnd = (**((DialogPeek)dlog)->textH).selEnd;
  103.                 SelIText(dlog,3,selStart,selEnd);                // Select text in invisible item
  104.                 DialogSelect(event,&evtDlog,itemHit);            // Input key
  105.                 SelIText(dlog,2,selStart,selEnd);                // Select same area in visible item
  106.                 if ((event->message & charCodeMask) != '\010')    // If it's not a backspace (backspace is the only key that can affect both the text and the selection- thus we need to process it in both fields, but not change it for the hidden field.
  107.                     event->message = '•';                        // Replace with character to use
  108.                 DialogSelect(event,&evtDlog,itemHit);            // Put in fake character
  109.                 return true;
  110.         }
  111.     }
  112.     
  113.     return false;            // For all non-keyDown events
  114. }
  115.  
  116. char *
  117. DifferentFontDialog()
  118. {    static char        password[256];
  119.     DialogPtr        dlog;
  120.     Handle            itemH;
  121.     short            item,itemType,font;
  122.     Rect            box;
  123.     
  124.     GetFNum("\p.Pwd",&font);        // Get the font number for our password font (it begins with a period, so AddResMenu won't add it)
  125.     SetDAFont(font);                // Use this font for static and edit text items in further dialogs
  126.     
  127.     dlog = GetNewDialog(rDifferentFontDialog,0L,(WindowPtr) -1L);
  128.     
  129.     GetDItem(dlog,3,&itemType,&itemH,&box);            // Because SetDAFont affects static items, too, we've got to use user items to draw our prompts
  130.     SetDItem(dlog,3,itemType,(Handle)ChicagoTextItem,&box);
  131.     GetDItem(dlog,4,&itemType,&itemH,&box);
  132.     SetDItem(dlog,4,itemType,(Handle)ChicagoTextItem,&box);
  133.     
  134.     do
  135.     {    ModalDialog(0L,&item);
  136.     } while (item != 1);            // Until the OK button is hit
  137.     
  138.     GetDItem(dlog,2,&itemType,&itemH,&box);        // Get text from TE item
  139.     GetIText(itemH,password);
  140.     
  141.     DisposDialog(dlog);
  142.     
  143.     SetDAFont(0);        // Set the dialog font back to the System font
  144.     
  145.     return password;
  146. }
  147.  
  148. pascal void
  149. ChicagoTextItem(WindowPtr wind,short item)
  150. {    short        fontStore,sizeStore;
  151.     Handle        itemH;
  152.     short        itemType;
  153.     Rect        box;
  154.     char        *text;
  155.     
  156.     SetPort(wind);
  157.     
  158.     fontStore = wind->txFont;    // Remember the current font & size
  159.     sizeStore = wind->txSize;
  160.     
  161.     TextFont(0);        // Set to default System font & size
  162.     TextSize(0);
  163.     
  164.     GetDItem(wind,item,&itemType,&itemH,&box);
  165.     
  166.     if (item == 3)            // These strings would probably be in a resource or somesuch in an actual program.
  167.         text = "\pPlease enter your password:";
  168.     if (item == 4)
  169.         text = "\pSpecial Font Password Dialog";
  170.     
  171.     TextBox(text+1,*text,&box,teJustLeft);    // Draw the prompt
  172.     
  173.     TextFont(fontStore);        // Restore the font & size
  174.     TextSize(sizeStore);
  175. }
  176.  
  177. char *
  178. InternalBufferDialog()
  179. {    static char        password[256];
  180.     DialogPtr        dlog;
  181.     short            item;
  182.     
  183.     dlog = GetNewDialog(rInternalBufferDialog,0L,(WindowPtr) -1L);
  184.     
  185.     *password = '\0';                    // Zero out the buffered password
  186.     SetWRefCon(dlog,(long)password);    // Stash the buffer's address
  187.     
  188.     do
  189.     {    ModalDialog(InternalBufferFilter,&item);
  190.     } while (item != 1);            // Until the OK button is hit
  191.     
  192.     DisposDialog(dlog);
  193.     
  194.     return password;
  195. }
  196.  
  197. pascal Boolean
  198. InternalBufferFilter(DialogPtr dlog,EventRecord *event,short *itemHit)
  199. {    char    key;
  200.     short    start,end;
  201.     char    *buffer;
  202.     
  203.     if (event->what != keyDown && event->what != autoKey)
  204.         return false;                // We don't want to deal with them
  205.     
  206.     key = event->message & charCodeMask;
  207.     
  208.     switch (key)
  209.     {    case '\n':            // Return
  210.         case '\003':        // Enter
  211.             *itemHit = 1;        // OK Button
  212.             return true;        // We handled the event
  213.         case '\t':            // Tab
  214.         case '\034':        // Left arrow
  215.         case '\035':        // Right arrow
  216.         case '\036':        // Up arrow
  217.         case '\037':        // Down arrow
  218.             return false;        // Let ModalDialog handle them
  219.         default:            // Everything else falls through to be dealt with
  220.             break;            //    below
  221.     }
  222.     
  223.     start = (**((DialogPeek)dlog)->textH).selStart;    // Get the current selection
  224.     end = (**((DialogPeek)dlog)->textH).selEnd;
  225.     
  226.     buffer = (char*)GetWRefCon(dlog);        // Get the buffer's address
  227.     
  228.     if (start != end)                    // If there's a selection, delete it
  229.         DeleteRange(buffer,start,end);
  230.     
  231.     if (key == '\010')    // Backspace
  232.     {    if (start != 0)
  233.             DeleteRange(buffer,start-1,start);    // Delete the character to the left
  234.     }
  235.     else
  236.     {    InsertChar(buffer,start,key);        // Insert the real key into the buffer
  237.         event->message = '*';                // Character to use in field
  238.     }
  239.     
  240.     return false;         // Let ModalDialog insert the fake char
  241. }
  242.  
  243. void
  244. DeleteRange(char *buffer,short start,short end)
  245. {    register char    *src,*dest,*last;
  246.     
  247.     last = buffer + *buffer;
  248.     
  249.     src = buffer + end + 1;
  250.     dest = buffer + start + 1;
  251.     
  252.     while (src <= last)            // Shift character to the left over the removed characters
  253.         *(dest++) = *(src++);
  254.     
  255.     (*buffer) -= (end-start);    // Adjust the buffer's length
  256. }
  257.  
  258. void
  259. InsertChar(char *buffer,short pos,char c)
  260. {    register short    index,len;
  261.     
  262.     len = *buffer;
  263.     
  264.     if (len == 0xFF)        // if the string is full, return
  265.         return;
  266.     
  267.     for (index = len;index > pos;index--)    // Shift characters to the right to make room
  268.         buffer[index+1] = buffer[index];
  269.     
  270.     buffer[pos+1] = c;        // Fill in the new character
  271.     
  272.     (*buffer)++;            // Add one to the length of the string
  273. }